#include "server_sql.h"
Server_Sql * Server_Sql::global_sql = nullptr;

Server_Sql::Server_Sql()
{
    Init_Sql();
}


void Server_Sql::Init_Sql()
{
    this->start(QThread::HighPriority);
}
void Server_Sql::process()
{

    if(TaskToDo.size()>0)
    {
        SQL_Task();
    }
    else
    {
        pause();
    }
}
void Server_Sql::AddTask(Inner_Msg *obj)
{
    qDebug()<<"SQL_Task_Add";
    TaskToDo.push_back(obj);
    if(this->state() == Paused)
    {
        resume();
    }
}
void Server_Sql::ReturnTask(Inner_Msg *obj)
{
    obj->SendToRelay();
}
void Server_Sql::SQL_Task()
{
    Inner_Msg* msg = TaskToDo.front();
    TaskToDo.pop_front();
    qDebug()<<"指令类型......................"<<msg->Msg_Type;
    QMap<QString,QString> &data = msg->Data;
    if(msg->Msg_Type == "POST_REIGSTER")
    {
        qDebug()<<"Sql_task";
        data.insert("AccountID", SQL::gethand()->registerAccount(data["passwd"], data["PhoneNumber"], data["name"]));
        data.insert("RES","SUCCESS");

        msg->SendToRelay();
        return;
    }
    if(msg->Msg_Type == "LIST_SYNC")
    {
        QVector<QString> GroupList;
        QVector<QString> FriendIDList;
        QVector<QString> FriendInfoList;
        QVector<QString> LIST;
        GroupList.clear();
        FriendIDList.clear();
        FriendInfoList.clear();
        SQL::gethand()->getGroupIdOnAccount(data["uid"],GroupList);
        qDebug()<<"GroupList size"<<GroupList.size();
        for(auto &groupID : GroupList)
        {
            FriendIDList.clear();
            FriendInfoList.clear();
            SQL::gethand()->getAccountFromGroup(groupID,FriendIDList);
            for(auto &friendID : FriendIDList)
            {
                 qDebug()<<"GROUP ID NUM"<<FriendIDList.size();
                FriendInfoList.push_back(ProJectData::Get_Usr(friendID,SQL::gethand()->getNickNameFromAccount(friendID)));
                qDebug()<<FriendInfoList.size();
            }
            qDebug()<<FriendInfoList.size();
            LIST.push_back(ProJectData::Get_LIST("POST_SYNC",SQL::gethand()->getGroupLeaderID(groupID),groupID, SQL::gethand()->getIsGroup(groupID), FriendInfoList));
        }
        data.insert("LIST_SYNC_DATA",ProJectData::Get_Download_LIST_SYNC(LIST));
        sleep(0);
        msg->SendToRelay();
        return;
    }
    if(msg->Msg_Type == "MSG_SYNC")
    {
        QVector<QString> hisList;
        QVector<QString> hisList_copy;
        if(!SQL::gethand()->getIsGroup(data["GID"]))
        {
            msg->SendToRelay();
            return;
        }

        SQL::gethand()->getHistory(data["GID"], hisList, 10);
        for(int i = 0;i<hisList.size();i++)
        {
            hisList_copy.push_back(hisList[hisList.size() -  i- 1]);
        }
        data.insert("MSG_SYNC_DATA", ProJectData::Get_Download_MSG_SYNC(data["GID"], SQL::gethand()->getIsGroup(data["GID"]) ,hisList_copy));
        msg->SendToRelay();
        return;
    }
    if(msg->Msg_Type == "POST_SYNC")
    {
        QVector<QString> applicationList;
        SQL::gethand()->getFriendApplication(data["uid"], applicationList);
        data.insert("POST_SYNC_DATA", ProJectData::Get_Download_POST_SYNC(applicationList));
        msg->SendToRelay();
        return;
    }

    if(msg->Msg_Type == "CREATE_GROUP")
    {
        SQL::gethand()->newGroup(data["uid"], true);
        msg->SendToRelay();
        return;
    }
    if(msg->Msg_Type == "PULL_TO_GROUP")
    {
        SQL::gethand()->addOneToGroup(data["GID"], data["uid"]);
        QVector<QString> uid_list;
        SQL::gethand()->getAccountFromGroup(data["GID"], uid_list);
        for(int i =0 ;i<uid_list.size();i++)
        {
            for(int j = 0;j<Server_Main::Online_Usr.size();j++)
            {
                if(uid_list[i] == Server_Main::Online_Usr[j]->uid)
                {
                     Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_LIST_SYNC(uid_list[i]));
                }
            }
        }
        msg->SendToRelay();
        return;
    }
    if(msg->Msg_Type == "ADD_TO_GROUP")
    {
        QString leder = SQL::gethand()->getGroupLeaderID(data["GID"]); //想加入的群
        QString name = SQL::gethand()->getNickNameFromAccount(data["uid"]); //想加入的人
        QString usrname = ProJectData::Get_Usr(data["uid"],name);
        QVector<QString> temp ;
        temp.append(usrname);
        SQL::gethand()->addFriendApplication(leder,ProJectData::Get_LIST("ADD_TO_GROUP",data["uid"],data["GID"],false,temp));
    }
    if(msg->Msg_Type == "AGREE_TO_GROUP")
    {
        QString leder = SQL::gethand()->getGroupLeaderID(data["GID"]);
        QString name = SQL::gethand()->getNickNameFromAccount(data["uid"]);
        QString usrname = ProJectData::Get_Usr(data["uid"],name);
        QVector<QString> temp ;
        temp.append(usrname);

        QString list = ProJectData::Get_LIST("ADD_TO_GROUP",data["uid"],data["GID"],false,temp);
        QJsonDocument QJson = QJsonDocument::fromJson(list.toUtf8());
        if(data["Status"] == "YES")
        {
            SQL::gethand()->addFriend(leder,data["uid"]);
        }

        QVector<QString> sqljson;
        SQL::gethand()->getFriendApplication(leder,sqljson);
        for(int i =0;i<sqljson.size();i++)
        {
            if(QJson == QJsonDocument::fromJson(sqljson[i].toUtf8()))
            {
                SQL::gethand()->removeFriendApplication(data["uid"],sqljson[i]);
            }
        }
    }
    if(msg->Msg_Type == "APPLY_FRIEND")
    {
        qDebug()<<"申请指令";
        QVector<QString> temp ;
        temp.clear();

        QString temp1 = ProJectData::Get_LIST("APPLAY_FRIEND",data["uid"],"-"+data["uid"],false,temp);
        SQL::gethand()->addFriendApplication(data["uid_friend"],temp1);

        for(int j = 0;j<Server_Main::Online_Usr.size();j++)
        {
            if(data["uid_friend"] == Server_Main::Online_Usr[j]->uid)
            {
                qDebug()<<"POST_SYNC";
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_POST_SYNC(data["uid_friend"]));
            }
        }
        sleep(1);
        for(int j = 0;j<Server_Main::Online_Usr.size();j++)
        {
            if(data["uid"] == Server_Main::Online_Usr[j]->uid)
            {
                qDebug()<<"POST_SYNC";
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_POST_SYNC(data["uid"]));
            }
        }

    }
    if(msg->Msg_Type == "ADD_FRIEND")
    {

        QString name = SQL::gethand()->getNickNameFromAccount(data["uid_friend"]);
        QString usrname = ProJectData::Get_Usr(data["uid_friend"],name);
        QVector<QString> temp ;
        temp.clear();

        QString temp1 = ProJectData::Get_LIST("APPLAY_FRIEND",data["uid_friend"],"-"+data["uid_friend"],false,temp);


        QJsonDocument QJson = QJsonDocument::fromJson(temp1.toUtf8());
        QVector<QString> sqljson;

        SQL::gethand()->getFriendApplication(data["uid"],sqljson);

        qDebug()<<"/*************/"<<data["Status"];
        if(data["Status"] == QString(true))
        {
            SQL::gethand()->addFriend(data["uid_friend"],data["uid"]);
        }
        for(int i = 0;i<sqljson.size();i++)
        {
            if(QJson == QJsonDocument::fromJson(sqljson[i].toUtf8()))
            {
                SQL::gethand()->removeFriendApplication(data["uid"],sqljson[i]);


            }
        }

        for(int j = 0;j<Server_Main::Online_Usr.size();j++)
        {
            if(data["uid_friend"] == Server_Main::Online_Usr[j]->uid)
            {
                qDebug()<<"POST_SYNC";
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_POST_SYNC(data["uid_friend"]));
                sleep(1);
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_LIST_SYNC(data["uid_friend"]));
            }
        }


        for(int j = 0;j<Server_Main::Online_Usr.size();j++)
        {
            if(data["uid"] == Server_Main::Online_Usr[j]->uid)
            {
                qDebug()<<"POST_SYNC";
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_POST_SYNC(data["uid"]));
                sleep(1);
                Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Upload_LIST_SYNC(data["uid_friend"]));
            }
        }


    }


    if(msg->Msg_Type == "LOGIN")
    {
        data.insert("RES", SQL::gethand()->LoginIn(data["uid"], data["passwd"], data["IP"], data["IP_CHECK"] == "YES"));
        if(SQL::gethand()->LoginIn(data["uid"], data["passwd"], data["IP"], data["IP_CHECK"] == "YES") == "LoginSucess")
        {
            data.insert("name",SQL::gethand()->getNickNameFromAccount(data["uid"]));
        }
        qDebug()<<"IP";
        qDebug()<<data["IP"];
        msg->SendToRelay();

        return;
    }
    if(msg->Msg_Type == "MSG_SEND")
    {
        SQL::gethand()->saveHistory(data["GID"], data["msg"], data["Time"]);
        QVector<QString> acc_list;
        SQL::gethand()->getAccountFromGroup(data["GID"], acc_list);
        for(int i = 0;i<acc_list.size();i++)
        {
            for(int j = 0;j<Server_Main::Online_Usr.size();j++)
            {
                if(acc_list[i] == Server_Main::Online_Usr[j]->uid&&Server_Main::Online_Usr[j]->uid!=data["uid_me"])
                {
                    qDebug()<<"MSG_RELAY";
                    QVector<QString> temp ;
                    temp.append(data["msg"]);
                    Server_Main::Online_Usr[j]->Get_Msg(ProJectData::Get_Download_MSG_UPDATE(SQL::gethand()->getIsGroup(data["GID"]),data["GID"],temp));
                }
            }

        }

        msg->SendToRelay();
        return;
    }
}

